//
//  CCLargeImageView.m
//  test02
//
//  Created by lzh on 2021/4/15.
//  Copyright © 2021 NO. All rights reserved.
//

#import "CCLargeImageView.h"
#import <UIImageView+WebCache.h>
#import "SDWebImageManager+largeImage.h"
@interface CCLargeImageView(){
     long long tiledCount;
     UIImage *originImage;
     CGRect imageRect;
     CGFloat imageScale_w;
     CGFloat imageScale_h;
}

@end

@implementation CCLargeImageView

/*
 round 如果参数是小数，则求本身的四舍五入.
 ceil 如果参数是小数，则求最小的整数但不小于本身.
 floor 如果参数是小数，则求最大的整数但不大于本身.
 -- round 3.000000
 -- ceil 4.000000
 -- floor 3.00000
 
 sqrt()开平方根。例如的16平方根为4。
 */
-(void)cc_setImageWithUrl:(NSString *)url{
    
    [self cc_setLargeImageWithUrl:url completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        if (finished && !error && image) {
            [self cc_setImage:image];
            }else {
             NSLog(@"YPLargeImageView error:%@",error);
        }
    }];
    
}
- (void)cc_setLargeImageWithUrl:(NSString *)url completed:(SDInternalCompletionBlock)completedBlock {
    
//    [[SDWebImageManager sharedManager]loadImageWithURL:[NSURL URLWithString:url] options:SDWebImageFromLoaderOnly progress:nil completed:completedBlock];
    
    [[SDWebImageManager sharedManager] loadImageWithURL:[NSURL URLWithString:url]
                                                             options:SDWebImageAvoidDecodeImage|SDWebImageScaleDownLargeImages
     progress:nil
    completed:completedBlock];
    
    
}

-(void)cc_setImage:(UIImage *)image{
    if (tiledCount==0) {
        tiledCount = 100;
    }
    originImage = image;
    [self setBackgroundColor:[UIColor whiteColor]];
    imageRect = CGRectMake(0.0f, 0.0f, CGImageGetWidth(originImage.CGImage), CGImageGetHeight(originImage.CGImage));
    imageScale_w = self.frame.size.width/imageRect.size.width;
    imageScale_h = self.frame.size.height/imageRect.size.height;
    CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
    
    int scale = (int)MAX(1/imageScale_w, 1/imageScale_h);
    int lev = ceil(scale);
    
    tiledLayer.levelsOfDetail = 1;
    tiledLayer.levelsOfDetailBias = lev;
    
    if (tiledCount>0) {
        NSInteger tileSizeScale = sqrt(tiledCount)/2.0;
        CGSize tileSize = self.bounds.size;
        tileSize.width /= tileSizeScale;
        tileSize.height /= tileSizeScale;
        tiledLayer.tileSize = tileSize;
    }
}
+(Class)layerClass{
    
    return [CATiledLayer class];
}
-(void)drawRect:(CGRect)rect{
    CGRect imageCutRect = CGRectMake(rect.origin.x/imageScale_w, rect.origin.y/imageScale_h, rect.size.width/imageScale_w, rect.size.height/imageScale_h);
    CGImageRef imageRef = CGImageCreateWithImageInRect(originImage.CGImage, imageCutRect);
    UIImage * tileImage = [UIImage imageWithCGImage:imageRef];
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIGraphicsPushContext(context);
    [tileImage drawInRect:rect];
    CGImageRelease(imageRef);
    UIGraphicsPopContext();
}
@end
